package com.example.myui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.example.myui.R;
import com.example.myui.fragment.Fragment1;
import com.example.myui.fragment.Fragment2;
import com.example.myui.fragment.Fragment3;
import com.example.myui.fragment.Fragment0;

import java.util.HashMap;

public class BottomnavBottonActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private HashMap<Integer, Fragment> fragments = new HashMap<>();
    public static final int ZERO = 0;
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    private int containerId = R.id.container;
    private int currentTab;
    private RadioButton btn0;
    private RadioButton btn1;
    private RadioButton btn2;
    private RadioButton btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottomnav_botton);

        initView();
    }

    private void initView() {
        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);

        btn0.setOnCheckedChangeListener(this);
        btn1.setOnCheckedChangeListener(this);
        btn2.setOnCheckedChangeListener(this);
        btn3.setOnCheckedChangeListener(this);

        fragments.put(ZERO, new Fragment0());
        fragments.put(ONE, new Fragment1());
        fragments.put(TWO, new Fragment2());
        fragments.put(THREE, new Fragment3());

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(containerId, fragments.get(ZERO));
        currentTab = ZERO;
        btn0.setChecked(true);
        fragmentTransaction.commit();

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.btn0:
                changeTab(ZERO);
                break;
            case R.id.btn1:
                changeTab(ONE);
                break;
            case R.id.btn2:
                changeTab(TWO);
                break;
            case R.id.btn3:
                changeTab(THREE);
                break;
            default:
                break;
        }
    }

    private void changeTab(int tab) {
        if (currentTab == tab) {
            return;
        }

        Fragment fragment = fragments.get(tab);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            ft.add(containerId, fragment);
        }

        ft.hide(fragments.get(currentTab));
        ft.show(fragments.get(tab));

        changeButtonStatus(currentTab, false);
        currentTab = tab;
        changeButtonStatus(currentTab, true);
        if (!this.isFinishing()) {
            ft.commitAllowingStateLoss();
        }
    }

    private void changeButtonStatus(int index, boolean b) {
        RadioButton button = null;
        switch (index) {
            case ZERO:
                button = btn0;
                break;
            case ONE:
                button = btn1;
                break;
            case TWO:
                button = btn2;
                break;
            case THREE:
                button = btn3;
                break;
        }
        button.setOnCheckedChangeListener(null);
        button.setChecked(b);
        button.setOnCheckedChangeListener(this);
        if (b) {
//            button.setTextColor(ContextCompat.getColor(this, R.color.colorAccent));
            button.setChecked(b);
        } else {
            button.setChecked(b);
//            button.setTextColor(ContextCompat.getColor(this, R.color.colorBlack));
        }

    }
}
